package components
{
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import mx.controls.Button;
	import mx.controls.ComboBox;
	import mx.controls.Label;
	import mx.controls.TextInput;
	import mx.events.ResizeEvent;
	
	import nl.wv.extenders.panel.SuperPanel;
	
	import tools.*;
	
	public class NewStudent{
		private var panel:SuperPanel = new SuperPanel();
		private var service:Mysql = new Mysql();
		private var service1:Mysql = new Mysql();
		private var service2:Mysql = new Mysql();
		private var service3:Mysql = new Mysql();
		private var tool:Tool = new Tool();
		private var labelUsername:Label = new Label();
		private var labelPassword:Label = new Label();
		private var labelSeparator:Label = new Label();
		private var labelBrojIndeksa:Label = new Label();
		private var labelSmjer:Label = new Label();
		private var labelIme:Label = new Label();
		private var labelPrezime:Label = new Label();
		private var comboBoxSmjer:ComboBox = new ComboBox();
		private var usernameTextField:TextInput = new TextInput();
		private var passwordTextField:TextInput = new TextInput();
		private var imeTextField:TextInput = new TextInput();
		private var prezimeTextField:TextInput = new TextInput();
		private var brojIndeksaTextField:TextInput = new TextInput();
		private var btn:Button = new Button();
		
		public function NewStudent(p:SuperPanel){
			panel = p;
			labelUsername.x = 10;
			labelUsername.y = 27;
			labelUsername.width = panel.width-30;
			labelUsername.height = 30;
			labelUsername.text = "Korisnicko ime: ";
			panel.addChild(labelUsername);
			
			labelPassword.x = 10;
			labelPassword.y = 47;
			labelPassword.width = panel.width-30;
			labelPassword.height = 30;
			labelPassword.text = "Sifra: ";
			panel.addChild(labelPassword);
			
			usernameTextField.x = 110;
			usernameTextField.y = 27;
			usernameTextField.width = panel.width-130;
			usernameTextField.height = 18;
			panel.addChild(usernameTextField);
			
			passwordTextField.x = 110;
			passwordTextField.y = 48;
			passwordTextField.width = panel.width-130;
			passwordTextField.height = 18;
			passwordTextField.displayAsPassword = true;
			panel.addChild(passwordTextField);
					
			labelSeparator.x = 10;
			labelSeparator.y = 62;
			labelSeparator.width = panel.width-30;
			labelSeparator.height = 30;
			labelSeparator.text = "---------------------------------------------------------";
			panel.addChild(labelSeparator);
			
			labelBrojIndeksa.x = 10;
			labelBrojIndeksa.y = 77;
			labelBrojIndeksa.width = panel.width-30;
			labelBrojIndeksa.height = 30;
			labelBrojIndeksa.text = "Ime: ";
			panel.addChild(labelBrojIndeksa);
			
			labelIme.x = 10;
			labelIme.y = 97;
			labelIme.width = panel.width-30;
			labelIme.height = 30;
			labelIme.text = "Prezime: ";
			panel.addChild(labelIme);

			labelPrezime.x = 10;
			labelPrezime.y = 117;
			labelPrezime.width = panel.width-30;
			labelPrezime.height = 30;
			labelPrezime.text = "Broj indeksa: ";
			panel.addChild(labelPrezime);
			
			labelSmjer.x = 10;
			labelSmjer.y = 137;
			labelSmjer.width = panel.width-30;
			labelSmjer.height = 30;
			labelSmjer.text = "Smjer: ";
			panel.addChild(labelSmjer);
			
			imeTextField.x = 110;
			imeTextField.y = 77;
			imeTextField.width = panel.width-130;
			imeTextField.height = 18;
			panel.addChild(imeTextField);
			
			prezimeTextField.x = 110;
			prezimeTextField.y = 97;
			prezimeTextField.width = panel.width-130;
			prezimeTextField.height = 18;
			panel.addChild(prezimeTextField);

			brojIndeksaTextField.x = 110;
			brojIndeksaTextField.y = 117;
			brojIndeksaTextField.width = panel.width-130;
			brojIndeksaTextField.height = 18;
			panel.addChild(brojIndeksaTextField);
			
			comboBoxSmjer.x = 110;
			comboBoxSmjer.y = 137;
			comboBoxSmjer.width = panel.width-130;
			comboBoxSmjer.height = 18;
			panel.addChild(comboBoxSmjer);
			
			btn.x = 10;
			btn.y = 157;
			btn.width = panel.width-30;
			btn.height = 18;
			btn.label = "Dodaj studenta";
			panel.addChild(btn);
			
			panel.addEventListener(ResizeEvent.RESIZE, onResize);
			btn.addEventListener(MouseEvent.CLICK, onClick);
			
			service3.sql = {type:"SELECT", a1:"s.id_smjer", a2:"s.naziv", from:"smjer s", where:"" };
			
	 		service3.addEventListener(service3.MYSQL_RESULT, onResultSmjer);
	 		service3.startService();
		}
		
		public function onResultSmjer(e:Event):void{
			var xmlList:XMLList = service3.xml.children();
			var xmlPom:String = "<root naziv='Smjerovi' tip='-1'>";
			for (var i:int; i<xmlList.length(); i++) {
				xmlPom+="<smjer id='"+xmlList[i].id_smjer+"' naziv='"+xmlList[i].naziv+"' />";
			}
			xmlPom+="</root>";
			var xml:XML=new XML(xmlPom);
			comboBoxSmjer.dataProvider = xml.children();
			comboBoxSmjer.labelField = "@naziv";
		}

		public function onResize(e:Event):void{
			labelUsername.width = panel.width-30;
			labelPassword.width = panel.width-30;
			usernameTextField.width = panel.width-130;
			passwordTextField.width = panel.width-130;
			labelSeparator.width = panel.width-30;
			labelBrojIndeksa.width = panel.width-30;
			labelIme.width = panel.width-30;
			labelPrezime.width = panel.width-30;
			labelSmjer.width = panel.width-30;
			imeTextField.width = panel.width-130;
			prezimeTextField.width = panel.width-130;
			brojIndeksaTextField.width = panel.width-130;
			comboBoxSmjer.width = panel.width-130;
			btn.width = panel.width-30;
		}
		
		public function onClick(e:MouseEvent):void{
			if(usernameTextField.text.length>0 && passwordTextField.text.length>0, imeTextField.text.length>0 && prezimeTextField.text.length>0 && brojIndeksaTextField.text.length>0){
            	service.sql = {type:"INSERT", into:"korisnik", columns:"username, password, tip", values:"@"+usernameTextField.text+"@,@"+passwordTextField.text+"@,@"+1+"@" }; 
				service.addEventListener(service.MYSQL_RESULT, onResult);
 				service.startService();
 			} else tool.createAlert("GRESKA", "Morate unijeti sva polja!");
		}
		
		public function onResult(e:Event):void{
			service1.sql = {type:"SELECT", a1:"k.id_username", from:"korisnik k", where:"k.id_username=(SELECT MAX(id_username) FROM korisnik k)" };
	 		service1.addEventListener(service1.MYSQL_RESULT, onResult1);
	 		service1.startService();
		}
		
		public function onResult1(e:Event):void{
            service2.sql = {type:"INSERT", into:"student", columns:"ime, prezime, brojIndeksa, username, smjer", values:"@"+imeTextField.text+"@,@"+prezimeTextField.text+"@,@"+brojIndeksaTextField.text+"@,@"+service1.xml.result.id_username+"@,@"+comboBoxSmjer.selectedItem.@id+"@" }; 
			service2.addEventListener(service1.MYSQL_RESULT, onResult2);
 			service2.startService();
		}
		
		public function onResult2(e:Event):void{
			tool.createAlert("OK", "Student je uspjesno upisan u bazu!");
			panel.closePanel();
		}
	}
}